package com.hxs.learn.action.core;

import java.util.List;

import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.entity.core.SysRoleMenu;
import com.hxs.learn.service.core.SysRoleMenuService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author huxinsheng
 * @date 2018/3/30 16:49
 */
@RestController
@RequestMapping("core/role/menu")
public class RoleMenuAction extends AbstractViewAction {
    private final SysRoleMenuService roleMenuService;

    @Autowired
    public RoleMenuAction(SysRoleMenuService roleMenuService) {
        this.roleMenuService = roleMenuService;
    }

    @ApiOperation(
        value = "加载角色菜单数据",
        notes = "角色菜单管理时显示加载角色菜单数据"
    )
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyRoleMenuData(@Login LoginInfo userInfo, SysRoleMenu sysRoleMenu) {
        return roleMenuService.saveRoleMenu(userInfo, sysRoleMenu);
    }

    @ApiOperation(
        value     = "编辑角色菜单页面",
        notes     = "角色管理时显示编辑角色菜单页面"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "roleId",
        value     = "角色ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping(ReqConst.REQ_MODIFY_ROLE_MENU_DIALOG)
    public ModelAndView modifyRoleMenuDialog(Model model, String roleId) {
        model.addAttribute(PAGE_DATA, roleId);

        return createModelAndView(ViewNames.PAGE_ROLE_MENU_MODIFY);
    }

    @ApiOperation(
        value     = "加载角色菜单数据",
        notes     = "角色菜单管理时显示加载角色菜单数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "roleId",
        value     = "角色ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping("getRoleMenuData")
    @ResponseBody
    public Result getRoleMenuData(String roleId) {
        List<TreeJson<Object>> roleMenuTreeJsons = roleMenuService.getRoleMenuTreeJson(roleId);

        return Result.ok(roleMenuTreeJsons);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
